﻿using Nancy;
using Nancy.Extensions;
using Nancy.ModelBinding;
using Nancy.Responses;
using Nancy.Security;
using RinNote.Server.Authentication;
using RinNote.Server.Controller.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RinNote.Server.Controller
{
    public class AccountController: NancyModule
    {
        public AccountController() : base("/Account")
        {

            //登录接口 
            //参数LoginModel对象
            Get["/login"] = parameters => {
                LoginModel Userpass = this.Bind<LoginModel>();
                if (string.IsNullOrWhiteSpace(Userpass.username) || string.IsNullOrWhiteSpace(Userpass.password))
                {
                    return new ApiResult() { IsSuccess = false, Message = "用户名或密码为空" };
                }
                var token = UserAuthorization.UserLogin(Userpass.username, Userpass.password,DateTime.Now.AddYears(10));
                if (token != null)
                {
                    return new ApiResult() {IsSuccess=true,Data=token };
                }
                return new ApiResult() { IsSuccess = false, Message="用户名或密码错误" }; ;
            };

            //登出接口
            Get["/LogOut"] = x => {
                UserAuthorization.UserLogout(this.Context);
                return new ApiResult() { IsSuccess = true };
            };
        }
    }
    class LoginModel
    {
        /// <summary>
        /// 用户名
        /// </summary>
        public string username { get; set; }
        /// <summary>
        /// 密码
        /// </summary>
        public string password { get; set; }
    }
}
